/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.wechat.api.controller.platform

* @Author          LuZhen

* @FileName        ComponentController

* @Date            2016/11/24

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 2016/11/24        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package com.yonyou.gmcc.gateway.wechat.controller.platform;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmcc.gateway.wechat.service.interfaces.AuthorityApiServiceInterface;

//@RestController
public class ComponentController {

    private static Logger logger = Logger.getLogger(ComponentController.class);


    @Autowired
    private AuthorityApiServiceInterface authorityApiServiceInterface;
    
    /**
    *
    * @author LiuJun
    * 接收第三方平台授权ticket
    * @date 2016年11月28日
    * @param nonce
    * @param timestamp
    * @param encryptType
    * @param msgSignature
    * @param xml
    * @throws Exception
    */
    //@RequestMapping(value = "/events", method =  RequestMethod.POST, produces = "text/plain;charset=UTF-8")
    public String saveTicket(@RequestParam("nonce") String nonce,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("encrypt_type") String encryptType,
                           @RequestParam("msg_signature") String msgSignature,
                           @RequestBody String xml) throws Exception{
        
        logger.info("接收第三方平台授权ticket-saveTicket is entry");
        logger.info("接收第三方平台授权ticket-xml="+xml);
        logger.info("接收第三方平台授权ticket-nonce="+nonce);
        logger.info("接收第三方平台授权ticket-timestamp="+timestamp);
        logger.info("接收第三方平台授权ticket-encrypt_type="+encryptType);
        logger.info("接收第三方平台授权ticket-msg_signature="+msgSignature);
        
        String result = authorityApiServiceInterface.saveTicket(nonce, timestamp, encryptType, msgSignature, xml);
        logger.info("saveTicket="+result);
        return result;
    }



    /**
     * 微信推送ticket
     */
    @RequestMapping(value = "/authorization/{dealerId}",method =  RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public String receiveAuthorization(@RequestParam("auth_code") String authCode,
                                @RequestParam("expires_in") Long expire,
                                @PathVariable("dealerId") String dealerId){
        //TODO 直接丢给授权微服务
        return "success";
    }

}
